/*******************************************************************************
* Master dofile for the preparation and analysis of HSW survey data
* This version: November 27, 2024
*******************************************************************************/



* Packages required ************************************************************
// ssc install estout
// ssc install mhtreg
// ssc install moremata
// ssc install missings 
// ssc install parmest


* All programs that need to be installed to be listed here together with install command (ssc install xxx)
local required_ados "moremata mhtreg estout missings parmest" //add the required ados here
	foreach x of local required_ados {
	capture findfile `x'.ado
	if _rc==601 {
		ssc install `x', replace
	}
}



* Set options
clear all
set more off
set seed 83
set maxvar 20000

* Set directories

global path "/Users/vojtabartos/Desktop/OWWA/241127_Replication" // This directory might be changed for different users. The other directories should not be changed

* Set globals
global dopath "$path/do" // set path to local github folder
global datapath "$path/data"
global data_input "$datapath/input"
global data_prep "$datapath/prepared"
global outpath "$path/output"
global outpath_fig "$outpath/figures"
global outpath_tab "$outpath/tables"


********************************************************************************
* Set globals
global controls     "finlit sharing account child max_highschool college age nointernet ksa baseline_mis direct_3 lduration_"
global controls3    "finlit sharing account child max_highschool college age nointernet ksa baseline_mis direct_3 li_duration_"
global controls_att "finlit sharing account child max_highschool college age nointernet ksa baseline_mis"

global esttab_opt_stats "stats(r2 N, fmt(2 0) labels("R2" "Observations")) label"
global esttab_opt_stats_ols_p "stats(base N, fmt(2 0) labels("Control mean" "Observations")) label booktabs nolines collabels(,none)"
global esttab_opt_stats_ols_f "stats(ia_pval r2 N, fmt(2 2 0) labels("F-Test (p-value)" "R2" "Observations")) label booktabs nolines collabels(,none)"

global cellsp "cells(b(fmt(3)) ci(par fmt(2)))"

********************************************************************************
* 1 Data preparation of baseline data (HSW and HH)
do "$dopath/prepare_baseline.do"

* 2 Data preparation of wave 2 proxy interviews
do "$dopath/prepare_hh_w2.do"

* 3 Data preparation of HSW wave 3 direct interviews 
do "$dopath/prepare_hsw_w3.do"

* 4 Data preparation of wave 3 proxy interviews
do "$dopath/prepare_proxy_w3.do"

* 5 Data preparation of HH wave 3 interviews
do "$dopath/prepare_hh_w3.do"

* 6 Prepare merged dataset
do "$dopath/prepare_merged_dataset.do"

********************************************************************************
* Load regout.ado
do "$dopath/regout.ado"

********************************************************************************
* Statistical analysis

* All of the following dofiles not commented out produce at least some results relevant for the paper tables.

* Create descriptive statistics for the balance tables
 do "$dopath/balance_descriptives.do"

* Analysis using pooled data
do "$dopath/analysis_field_experiment.do"

* Analysis of educational outcomes (individuals in household)
do "$dopath/analysis_education.do"

* Figures for treatment effects by duration since baseline
do "$dopath/effect_by_duration.do"

